package com.cat.dataStructure03;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/put-marbles-in-bags/
 * @create 2025/11/16 10:01
 * @since JDK17
 */

class Solution65 {
    public long putMarbles(int[] weights, int k) {
        int n = weights.length;
        for (int i = 0; i < n - 1; i++) {
            weights[i] += weights[i + 1];
        }
        Arrays.sort(weights, 0, n - 1);
        long ans = 0;
        for (int i = 0; i < k - 1; i++) {
            ans += weights[n - 2 - i] - weights[i];
        }

        return ans;
    }
}